/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.games.epkb.controller;

import it.polimi.games.epkb.controller.exceptions.IllegalOrphanException;
import it.polimi.games.epkb.controller.exceptions.NonexistentEntityException;
import it.polimi.games.epkb.controller.exceptions.PreexistingEntityException;
import it.polimi.games.epkb.entityClasses.GpiKpi;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import it.polimi.games.epkb.entityClasses.Predicate;
import java.util.ArrayList;
import java.util.Collection;
import it.polimi.games.epkb.entityClasses.Consequent;
import it.polimi.games.epkb.entityClasses.Thresholds;
import it.polimi.games.epkb.entityClasses.GpiKpiInstance;
import it.polimi.games.epkb.entityClasses.ExecutedRules;
import it.polimi.games.epkb.entityClasses.Antecedent;
import it.polimi.games.epkb.entityClasses.Correlation;
import it.polimi.games.epkb.entityClasses.StoredValues;
import javax.transaction.UserTransaction;

/**
 *
 * @author plebani
 */
public class GpiKpiJpaController implements Serializable {

    public GpiKpiJpaController(EntityManagerFactory emf) {
        this.utx = null;
        this.emf = emf;
    }
    public GpiKpiJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(GpiKpi gpiKpi) throws PreexistingEntityException, Exception {
        if (gpiKpi.getPredicateCollection() == null) {
            gpiKpi.setPredicateCollection(new ArrayList<Predicate>());
        }
        if (gpiKpi.getConsequentCollection() == null) {
            gpiKpi.setConsequentCollection(new ArrayList<Consequent>());
        }
        if (gpiKpi.getThresholdsCollection() == null) {
            gpiKpi.setThresholdsCollection(new ArrayList<Thresholds>());
        }
        if (gpiKpi.getGpiKpiInstanceCollection() == null) {
            gpiKpi.setGpiKpiInstanceCollection(new ArrayList<GpiKpiInstance>());
        }
        if (gpiKpi.getGpiKpiInstanceCollection1() == null) {
            gpiKpi.setGpiKpiInstanceCollection1(new ArrayList<GpiKpiInstance>());
        }
        if (gpiKpi.getExecutedRulesCollection() == null) {
            gpiKpi.setExecutedRulesCollection(new ArrayList<ExecutedRules>());
        }
        if (gpiKpi.getAntecedentCollection() == null) {
            gpiKpi.setAntecedentCollection(new ArrayList<Antecedent>());
        }
        if (gpiKpi.getCorrelationCollection() == null) {
            gpiKpi.setCorrelationCollection(new ArrayList<Correlation>());
        }
        if (gpiKpi.getCorrelationCollection1() == null) {
            gpiKpi.setCorrelationCollection1(new ArrayList<Correlation>());
        }
        if (gpiKpi.getStoredValuesCollection() == null) {
            gpiKpi.setStoredValuesCollection(new ArrayList<StoredValues>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Predicate> attachedPredicateCollection = new ArrayList<Predicate>();
            for (Predicate predicateCollectionPredicateToAttach : gpiKpi.getPredicateCollection()) {
                predicateCollectionPredicateToAttach = em.getReference(predicateCollectionPredicateToAttach.getClass(), predicateCollectionPredicateToAttach.getId());
                attachedPredicateCollection.add(predicateCollectionPredicateToAttach);
            }
            gpiKpi.setPredicateCollection(attachedPredicateCollection);
            Collection<Consequent> attachedConsequentCollection = new ArrayList<Consequent>();
            for (Consequent consequentCollectionConsequentToAttach : gpiKpi.getConsequentCollection()) {
                consequentCollectionConsequentToAttach = em.getReference(consequentCollectionConsequentToAttach.getClass(), consequentCollectionConsequentToAttach.getConsequentPK());
                attachedConsequentCollection.add(consequentCollectionConsequentToAttach);
            }
            gpiKpi.setConsequentCollection(attachedConsequentCollection);
            Collection<Thresholds> attachedThresholdsCollection = new ArrayList<Thresholds>();
            for (Thresholds thresholdsCollectionThresholdsToAttach : gpiKpi.getThresholdsCollection()) {
                thresholdsCollectionThresholdsToAttach = em.getReference(thresholdsCollectionThresholdsToAttach.getClass(), thresholdsCollectionThresholdsToAttach.getThresholdsPK());
                attachedThresholdsCollection.add(thresholdsCollectionThresholdsToAttach);
            }
            gpiKpi.setThresholdsCollection(attachedThresholdsCollection);
            Collection<GpiKpiInstance> attachedGpiKpiInstanceCollection = new ArrayList<GpiKpiInstance>();
            for (GpiKpiInstance gpiKpiInstanceCollectionGpiKpiInstanceToAttach : gpiKpi.getGpiKpiInstanceCollection()) {
                gpiKpiInstanceCollectionGpiKpiInstanceToAttach = em.getReference(gpiKpiInstanceCollectionGpiKpiInstanceToAttach.getClass(), gpiKpiInstanceCollectionGpiKpiInstanceToAttach.getGpiKpiInstancePK());
                attachedGpiKpiInstanceCollection.add(gpiKpiInstanceCollectionGpiKpiInstanceToAttach);
            }
            gpiKpi.setGpiKpiInstanceCollection(attachedGpiKpiInstanceCollection);
            Collection<GpiKpiInstance> attachedGpiKpiInstanceCollection1 = new ArrayList<GpiKpiInstance>();
            for (GpiKpiInstance gpiKpiInstanceCollection1GpiKpiInstanceToAttach : gpiKpi.getGpiKpiInstanceCollection1()) {
                gpiKpiInstanceCollection1GpiKpiInstanceToAttach = em.getReference(gpiKpiInstanceCollection1GpiKpiInstanceToAttach.getClass(), gpiKpiInstanceCollection1GpiKpiInstanceToAttach.getGpiKpiInstancePK());
                attachedGpiKpiInstanceCollection1.add(gpiKpiInstanceCollection1GpiKpiInstanceToAttach);
            }
            gpiKpi.setGpiKpiInstanceCollection1(attachedGpiKpiInstanceCollection1);
            Collection<ExecutedRules> attachedExecutedRulesCollection = new ArrayList<ExecutedRules>();
            for (ExecutedRules executedRulesCollectionExecutedRulesToAttach : gpiKpi.getExecutedRulesCollection()) {
                executedRulesCollectionExecutedRulesToAttach = em.getReference(executedRulesCollectionExecutedRulesToAttach.getClass(), executedRulesCollectionExecutedRulesToAttach.getId());
                attachedExecutedRulesCollection.add(executedRulesCollectionExecutedRulesToAttach);
            }
            gpiKpi.setExecutedRulesCollection(attachedExecutedRulesCollection);
            Collection<Antecedent> attachedAntecedentCollection = new ArrayList<Antecedent>();
            for (Antecedent antecedentCollectionAntecedentToAttach : gpiKpi.getAntecedentCollection()) {
                antecedentCollectionAntecedentToAttach = em.getReference(antecedentCollectionAntecedentToAttach.getClass(), antecedentCollectionAntecedentToAttach.getAntecedentPK());
                attachedAntecedentCollection.add(antecedentCollectionAntecedentToAttach);
            }
            gpiKpi.setAntecedentCollection(attachedAntecedentCollection);
            Collection<Correlation> attachedCorrelationCollection = new ArrayList<Correlation>();
            for (Correlation correlationCollectionCorrelationToAttach : gpiKpi.getCorrelationCollection()) {
                correlationCollectionCorrelationToAttach = em.getReference(correlationCollectionCorrelationToAttach.getClass(), correlationCollectionCorrelationToAttach.getId());
                attachedCorrelationCollection.add(correlationCollectionCorrelationToAttach);
            }
            gpiKpi.setCorrelationCollection(attachedCorrelationCollection);
            Collection<Correlation> attachedCorrelationCollection1 = new ArrayList<Correlation>();
            for (Correlation correlationCollection1CorrelationToAttach : gpiKpi.getCorrelationCollection1()) {
                correlationCollection1CorrelationToAttach = em.getReference(correlationCollection1CorrelationToAttach.getClass(), correlationCollection1CorrelationToAttach.getId());
                attachedCorrelationCollection1.add(correlationCollection1CorrelationToAttach);
            }
            gpiKpi.setCorrelationCollection1(attachedCorrelationCollection1);
            Collection<StoredValues> attachedStoredValuesCollection = new ArrayList<StoredValues>();
            for (StoredValues storedValuesCollectionStoredValuesToAttach : gpiKpi.getStoredValuesCollection()) {
                storedValuesCollectionStoredValuesToAttach = em.getReference(storedValuesCollectionStoredValuesToAttach.getClass(), storedValuesCollectionStoredValuesToAttach.getId());
                attachedStoredValuesCollection.add(storedValuesCollectionStoredValuesToAttach);
            }
            gpiKpi.setStoredValuesCollection(attachedStoredValuesCollection);
            em.persist(gpiKpi);
            for (Predicate predicateCollectionPredicate : gpiKpi.getPredicateCollection()) {
                predicateCollectionPredicate.getGpiKpiCollection().add(gpiKpi);
                predicateCollectionPredicate = em.merge(predicateCollectionPredicate);
            }
            for (Consequent consequentCollectionConsequent : gpiKpi.getConsequentCollection()) {
                GpiKpi oldGpiKpiOfConsequentCollectionConsequent = consequentCollectionConsequent.getGpiKpi();
                consequentCollectionConsequent.setGpiKpi(gpiKpi);
                consequentCollectionConsequent = em.merge(consequentCollectionConsequent);
                if (oldGpiKpiOfConsequentCollectionConsequent != null) {
                    oldGpiKpiOfConsequentCollectionConsequent.getConsequentCollection().remove(consequentCollectionConsequent);
                    oldGpiKpiOfConsequentCollectionConsequent = em.merge(oldGpiKpiOfConsequentCollectionConsequent);
                }
            }
            for (Thresholds thresholdsCollectionThresholds : gpiKpi.getThresholdsCollection()) {
                GpiKpi oldGpiKpiOfThresholdsCollectionThresholds = thresholdsCollectionThresholds.getGpiKpi();
                thresholdsCollectionThresholds.setGpiKpi(gpiKpi);
                thresholdsCollectionThresholds = em.merge(thresholdsCollectionThresholds);
                if (oldGpiKpiOfThresholdsCollectionThresholds != null) {
                    oldGpiKpiOfThresholdsCollectionThresholds.getThresholdsCollection().remove(thresholdsCollectionThresholds);
                    oldGpiKpiOfThresholdsCollectionThresholds = em.merge(oldGpiKpiOfThresholdsCollectionThresholds);
                }
            }
            for (GpiKpiInstance gpiKpiInstanceCollectionGpiKpiInstance : gpiKpi.getGpiKpiInstanceCollection()) {
                GpiKpi oldIdGPIKPIOfGpiKpiInstanceCollectionGpiKpiInstance = gpiKpiInstanceCollectionGpiKpiInstance.getIdGPIKPI();
                gpiKpiInstanceCollectionGpiKpiInstance.setIdGPIKPI(gpiKpi);
                gpiKpiInstanceCollectionGpiKpiInstance = em.merge(gpiKpiInstanceCollectionGpiKpiInstance);
                if (oldIdGPIKPIOfGpiKpiInstanceCollectionGpiKpiInstance != null) {
                    oldIdGPIKPIOfGpiKpiInstanceCollectionGpiKpiInstance.getGpiKpiInstanceCollection().remove(gpiKpiInstanceCollectionGpiKpiInstance);
                    oldIdGPIKPIOfGpiKpiInstanceCollectionGpiKpiInstance = em.merge(oldIdGPIKPIOfGpiKpiInstanceCollectionGpiKpiInstance);
                }
            }
            for (GpiKpiInstance gpiKpiInstanceCollection1GpiKpiInstance : gpiKpi.getGpiKpiInstanceCollection1()) {
                GpiKpi oldIdReferredGPIKPIOfGpiKpiInstanceCollection1GpiKpiInstance = gpiKpiInstanceCollection1GpiKpiInstance.getIdReferredGPIKPI();
                gpiKpiInstanceCollection1GpiKpiInstance.setIdReferredGPIKPI(gpiKpi);
                gpiKpiInstanceCollection1GpiKpiInstance = em.merge(gpiKpiInstanceCollection1GpiKpiInstance);
                if (oldIdReferredGPIKPIOfGpiKpiInstanceCollection1GpiKpiInstance != null) {
                    oldIdReferredGPIKPIOfGpiKpiInstanceCollection1GpiKpiInstance.getGpiKpiInstanceCollection1().remove(gpiKpiInstanceCollection1GpiKpiInstance);
                    oldIdReferredGPIKPIOfGpiKpiInstanceCollection1GpiKpiInstance = em.merge(oldIdReferredGPIKPIOfGpiKpiInstanceCollection1GpiKpiInstance);
                }
            }
            for (ExecutedRules executedRulesCollectionExecutedRules : gpiKpi.getExecutedRulesCollection()) {
                GpiKpi oldIdGPIKPIViolatedOfExecutedRulesCollectionExecutedRules = executedRulesCollectionExecutedRules.getIdGPIKPIViolated();
                executedRulesCollectionExecutedRules.setIdGPIKPIViolated(gpiKpi);
                executedRulesCollectionExecutedRules = em.merge(executedRulesCollectionExecutedRules);
                if (oldIdGPIKPIViolatedOfExecutedRulesCollectionExecutedRules != null) {
                    oldIdGPIKPIViolatedOfExecutedRulesCollectionExecutedRules.getExecutedRulesCollection().remove(executedRulesCollectionExecutedRules);
                    oldIdGPIKPIViolatedOfExecutedRulesCollectionExecutedRules = em.merge(oldIdGPIKPIViolatedOfExecutedRulesCollectionExecutedRules);
                }
            }
            for (Antecedent antecedentCollectionAntecedent : gpiKpi.getAntecedentCollection()) {
                GpiKpi oldGpiKpiOfAntecedentCollectionAntecedent = antecedentCollectionAntecedent.getGpiKpi();
                antecedentCollectionAntecedent.setGpiKpi(gpiKpi);
                antecedentCollectionAntecedent = em.merge(antecedentCollectionAntecedent);
                if (oldGpiKpiOfAntecedentCollectionAntecedent != null) {
                    oldGpiKpiOfAntecedentCollectionAntecedent.getAntecedentCollection().remove(antecedentCollectionAntecedent);
                    oldGpiKpiOfAntecedentCollectionAntecedent = em.merge(oldGpiKpiOfAntecedentCollectionAntecedent);
                }
            }
            for (Correlation correlationCollectionCorrelation : gpiKpi.getCorrelationCollection()) {
                GpiKpi oldIdGPIKPI2OfCorrelationCollectionCorrelation = correlationCollectionCorrelation.getIdGPIKPI2();
                correlationCollectionCorrelation.setIdGPIKPI2(gpiKpi);
                correlationCollectionCorrelation = em.merge(correlationCollectionCorrelation);
                if (oldIdGPIKPI2OfCorrelationCollectionCorrelation != null) {
                    oldIdGPIKPI2OfCorrelationCollectionCorrelation.getCorrelationCollection().remove(correlationCollectionCorrelation);
                    oldIdGPIKPI2OfCorrelationCollectionCorrelation = em.merge(oldIdGPIKPI2OfCorrelationCollectionCorrelation);
                }
            }
            for (Correlation correlationCollection1Correlation : gpiKpi.getCorrelationCollection1()) {
                GpiKpi oldIdGPIKPI1OfCorrelationCollection1Correlation = correlationCollection1Correlation.getIdGPIKPI1();
                correlationCollection1Correlation.setIdGPIKPI1(gpiKpi);
                correlationCollection1Correlation = em.merge(correlationCollection1Correlation);
                if (oldIdGPIKPI1OfCorrelationCollection1Correlation != null) {
                    oldIdGPIKPI1OfCorrelationCollection1Correlation.getCorrelationCollection1().remove(correlationCollection1Correlation);
                    oldIdGPIKPI1OfCorrelationCollection1Correlation = em.merge(oldIdGPIKPI1OfCorrelationCollection1Correlation);
                }
            }
            for (StoredValues storedValuesCollectionStoredValues : gpiKpi.getStoredValuesCollection()) {
                GpiKpi oldIdGPIKPIOfStoredValuesCollectionStoredValues = storedValuesCollectionStoredValues.getIdGPIKPI();
                storedValuesCollectionStoredValues.setIdGPIKPI(gpiKpi);
                storedValuesCollectionStoredValues = em.merge(storedValuesCollectionStoredValues);
                if (oldIdGPIKPIOfStoredValuesCollectionStoredValues != null) {
                    oldIdGPIKPIOfStoredValuesCollectionStoredValues.getStoredValuesCollection().remove(storedValuesCollectionStoredValues);
                    oldIdGPIKPIOfStoredValuesCollectionStoredValues = em.merge(oldIdGPIKPIOfStoredValuesCollectionStoredValues);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findGpiKpi(gpiKpi.getId()) != null) {
                throw new PreexistingEntityException("GpiKpi " + gpiKpi + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(GpiKpi gpiKpi) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            GpiKpi persistentGpiKpi = em.find(GpiKpi.class, gpiKpi.getId());
            Collection<Predicate> predicateCollectionOld = persistentGpiKpi.getPredicateCollection();
            Collection<Predicate> predicateCollectionNew = gpiKpi.getPredicateCollection();
            Collection<Consequent> consequentCollectionOld = persistentGpiKpi.getConsequentCollection();
            Collection<Consequent> consequentCollectionNew = gpiKpi.getConsequentCollection();
            Collection<Thresholds> thresholdsCollectionOld = persistentGpiKpi.getThresholdsCollection();
            Collection<Thresholds> thresholdsCollectionNew = gpiKpi.getThresholdsCollection();
            Collection<GpiKpiInstance> gpiKpiInstanceCollectionOld = persistentGpiKpi.getGpiKpiInstanceCollection();
            Collection<GpiKpiInstance> gpiKpiInstanceCollectionNew = gpiKpi.getGpiKpiInstanceCollection();
            Collection<GpiKpiInstance> gpiKpiInstanceCollection1Old = persistentGpiKpi.getGpiKpiInstanceCollection1();
            Collection<GpiKpiInstance> gpiKpiInstanceCollection1New = gpiKpi.getGpiKpiInstanceCollection1();
            Collection<ExecutedRules> executedRulesCollectionOld = persistentGpiKpi.getExecutedRulesCollection();
            Collection<ExecutedRules> executedRulesCollectionNew = gpiKpi.getExecutedRulesCollection();
            Collection<Antecedent> antecedentCollectionOld = persistentGpiKpi.getAntecedentCollection();
            Collection<Antecedent> antecedentCollectionNew = gpiKpi.getAntecedentCollection();
            Collection<Correlation> correlationCollectionOld = persistentGpiKpi.getCorrelationCollection();
            Collection<Correlation> correlationCollectionNew = gpiKpi.getCorrelationCollection();
            Collection<Correlation> correlationCollection1Old = persistentGpiKpi.getCorrelationCollection1();
            Collection<Correlation> correlationCollection1New = gpiKpi.getCorrelationCollection1();
            Collection<StoredValues> storedValuesCollectionOld = persistentGpiKpi.getStoredValuesCollection();
            Collection<StoredValues> storedValuesCollectionNew = gpiKpi.getStoredValuesCollection();
            List<String> illegalOrphanMessages = null;
            for (Consequent consequentCollectionOldConsequent : consequentCollectionOld) {
                if (!consequentCollectionNew.contains(consequentCollectionOldConsequent)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Consequent " + consequentCollectionOldConsequent + " since its gpiKpi field is not nullable.");
                }
            }
            for (Thresholds thresholdsCollectionOldThresholds : thresholdsCollectionOld) {
                if (!thresholdsCollectionNew.contains(thresholdsCollectionOldThresholds)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Thresholds " + thresholdsCollectionOldThresholds + " since its gpiKpi field is not nullable.");
                }
            }
            for (GpiKpiInstance gpiKpiInstanceCollectionOldGpiKpiInstance : gpiKpiInstanceCollectionOld) {
                if (!gpiKpiInstanceCollectionNew.contains(gpiKpiInstanceCollectionOldGpiKpiInstance)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain GpiKpiInstance " + gpiKpiInstanceCollectionOldGpiKpiInstance + " since its idGPIKPI field is not nullable.");
                }
            }
            for (ExecutedRules executedRulesCollectionOldExecutedRules : executedRulesCollectionOld) {
                if (!executedRulesCollectionNew.contains(executedRulesCollectionOldExecutedRules)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain ExecutedRules " + executedRulesCollectionOldExecutedRules + " since its idGPIKPIViolated field is not nullable.");
                }
            }
            for (Antecedent antecedentCollectionOldAntecedent : antecedentCollectionOld) {
                if (!antecedentCollectionNew.contains(antecedentCollectionOldAntecedent)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Antecedent " + antecedentCollectionOldAntecedent + " since its gpiKpi field is not nullable.");
                }
            }
            for (Correlation correlationCollectionOldCorrelation : correlationCollectionOld) {
                if (!correlationCollectionNew.contains(correlationCollectionOldCorrelation)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Correlation " + correlationCollectionOldCorrelation + " since its idGPIKPI2 field is not nullable.");
                }
            }
            for (Correlation correlationCollection1OldCorrelation : correlationCollection1Old) {
                if (!correlationCollection1New.contains(correlationCollection1OldCorrelation)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Correlation " + correlationCollection1OldCorrelation + " since its idGPIKPI1 field is not nullable.");
                }
            }
            for (StoredValues storedValuesCollectionOldStoredValues : storedValuesCollectionOld) {
                if (!storedValuesCollectionNew.contains(storedValuesCollectionOldStoredValues)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain StoredValues " + storedValuesCollectionOldStoredValues + " since its idGPIKPI field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Predicate> attachedPredicateCollectionNew = new ArrayList<Predicate>();
            for (Predicate predicateCollectionNewPredicateToAttach : predicateCollectionNew) {
                predicateCollectionNewPredicateToAttach = em.getReference(predicateCollectionNewPredicateToAttach.getClass(), predicateCollectionNewPredicateToAttach.getId());
                attachedPredicateCollectionNew.add(predicateCollectionNewPredicateToAttach);
            }
            predicateCollectionNew = attachedPredicateCollectionNew;
            gpiKpi.setPredicateCollection(predicateCollectionNew);
            Collection<Consequent> attachedConsequentCollectionNew = new ArrayList<Consequent>();
            for (Consequent consequentCollectionNewConsequentToAttach : consequentCollectionNew) {
                consequentCollectionNewConsequentToAttach = em.getReference(consequentCollectionNewConsequentToAttach.getClass(), consequentCollectionNewConsequentToAttach.getConsequentPK());
                attachedConsequentCollectionNew.add(consequentCollectionNewConsequentToAttach);
            }
            consequentCollectionNew = attachedConsequentCollectionNew;
            gpiKpi.setConsequentCollection(consequentCollectionNew);
            Collection<Thresholds> attachedThresholdsCollectionNew = new ArrayList<Thresholds>();
            for (Thresholds thresholdsCollectionNewThresholdsToAttach : thresholdsCollectionNew) {
                thresholdsCollectionNewThresholdsToAttach = em.getReference(thresholdsCollectionNewThresholdsToAttach.getClass(), thresholdsCollectionNewThresholdsToAttach.getThresholdsPK());
                attachedThresholdsCollectionNew.add(thresholdsCollectionNewThresholdsToAttach);
            }
            thresholdsCollectionNew = attachedThresholdsCollectionNew;
            gpiKpi.setThresholdsCollection(thresholdsCollectionNew);
            Collection<GpiKpiInstance> attachedGpiKpiInstanceCollectionNew = new ArrayList<GpiKpiInstance>();
            for (GpiKpiInstance gpiKpiInstanceCollectionNewGpiKpiInstanceToAttach : gpiKpiInstanceCollectionNew) {
                gpiKpiInstanceCollectionNewGpiKpiInstanceToAttach = em.getReference(gpiKpiInstanceCollectionNewGpiKpiInstanceToAttach.getClass(), gpiKpiInstanceCollectionNewGpiKpiInstanceToAttach.getGpiKpiInstancePK());
                attachedGpiKpiInstanceCollectionNew.add(gpiKpiInstanceCollectionNewGpiKpiInstanceToAttach);
            }
            gpiKpiInstanceCollectionNew = attachedGpiKpiInstanceCollectionNew;
            gpiKpi.setGpiKpiInstanceCollection(gpiKpiInstanceCollectionNew);
            Collection<GpiKpiInstance> attachedGpiKpiInstanceCollection1New = new ArrayList<GpiKpiInstance>();
            for (GpiKpiInstance gpiKpiInstanceCollection1NewGpiKpiInstanceToAttach : gpiKpiInstanceCollection1New) {
                gpiKpiInstanceCollection1NewGpiKpiInstanceToAttach = em.getReference(gpiKpiInstanceCollection1NewGpiKpiInstanceToAttach.getClass(), gpiKpiInstanceCollection1NewGpiKpiInstanceToAttach.getGpiKpiInstancePK());
                attachedGpiKpiInstanceCollection1New.add(gpiKpiInstanceCollection1NewGpiKpiInstanceToAttach);
            }
            gpiKpiInstanceCollection1New = attachedGpiKpiInstanceCollection1New;
            gpiKpi.setGpiKpiInstanceCollection1(gpiKpiInstanceCollection1New);
            Collection<ExecutedRules> attachedExecutedRulesCollectionNew = new ArrayList<ExecutedRules>();
            for (ExecutedRules executedRulesCollectionNewExecutedRulesToAttach : executedRulesCollectionNew) {
                executedRulesCollectionNewExecutedRulesToAttach = em.getReference(executedRulesCollectionNewExecutedRulesToAttach.getClass(), executedRulesCollectionNewExecutedRulesToAttach.getId());
                attachedExecutedRulesCollectionNew.add(executedRulesCollectionNewExecutedRulesToAttach);
            }
            executedRulesCollectionNew = attachedExecutedRulesCollectionNew;
            gpiKpi.setExecutedRulesCollection(executedRulesCollectionNew);
            Collection<Antecedent> attachedAntecedentCollectionNew = new ArrayList<Antecedent>();
            for (Antecedent antecedentCollectionNewAntecedentToAttach : antecedentCollectionNew) {
                antecedentCollectionNewAntecedentToAttach = em.getReference(antecedentCollectionNewAntecedentToAttach.getClass(), antecedentCollectionNewAntecedentToAttach.getAntecedentPK());
                attachedAntecedentCollectionNew.add(antecedentCollectionNewAntecedentToAttach);
            }
            antecedentCollectionNew = attachedAntecedentCollectionNew;
            gpiKpi.setAntecedentCollection(antecedentCollectionNew);
            Collection<Correlation> attachedCorrelationCollectionNew = new ArrayList<Correlation>();
            for (Correlation correlationCollectionNewCorrelationToAttach : correlationCollectionNew) {
                correlationCollectionNewCorrelationToAttach = em.getReference(correlationCollectionNewCorrelationToAttach.getClass(), correlationCollectionNewCorrelationToAttach.getId());
                attachedCorrelationCollectionNew.add(correlationCollectionNewCorrelationToAttach);
            }
            correlationCollectionNew = attachedCorrelationCollectionNew;
            gpiKpi.setCorrelationCollection(correlationCollectionNew);
            Collection<Correlation> attachedCorrelationCollection1New = new ArrayList<Correlation>();
            for (Correlation correlationCollection1NewCorrelationToAttach : correlationCollection1New) {
                correlationCollection1NewCorrelationToAttach = em.getReference(correlationCollection1NewCorrelationToAttach.getClass(), correlationCollection1NewCorrelationToAttach.getId());
                attachedCorrelationCollection1New.add(correlationCollection1NewCorrelationToAttach);
            }
            correlationCollection1New = attachedCorrelationCollection1New;
            gpiKpi.setCorrelationCollection1(correlationCollection1New);
            Collection<StoredValues> attachedStoredValuesCollectionNew = new ArrayList<StoredValues>();
            for (StoredValues storedValuesCollectionNewStoredValuesToAttach : storedValuesCollectionNew) {
                storedValuesCollectionNewStoredValuesToAttach = em.getReference(storedValuesCollectionNewStoredValuesToAttach.getClass(), storedValuesCollectionNewStoredValuesToAttach.getId());
                attachedStoredValuesCollectionNew.add(storedValuesCollectionNewStoredValuesToAttach);
            }
            storedValuesCollectionNew = attachedStoredValuesCollectionNew;
            gpiKpi.setStoredValuesCollection(storedValuesCollectionNew);
            gpiKpi = em.merge(gpiKpi);
            for (Predicate predicateCollectionOldPredicate : predicateCollectionOld) {
                if (!predicateCollectionNew.contains(predicateCollectionOldPredicate)) {
                    predicateCollectionOldPredicate.getGpiKpiCollection().remove(gpiKpi);
                    predicateCollectionOldPredicate = em.merge(predicateCollectionOldPredicate);
                }
            }
            for (Predicate predicateCollectionNewPredicate : predicateCollectionNew) {
                if (!predicateCollectionOld.contains(predicateCollectionNewPredicate)) {
                    predicateCollectionNewPredicate.getGpiKpiCollection().add(gpiKpi);
                    predicateCollectionNewPredicate = em.merge(predicateCollectionNewPredicate);
                }
            }
            for (Consequent consequentCollectionNewConsequent : consequentCollectionNew) {
                if (!consequentCollectionOld.contains(consequentCollectionNewConsequent)) {
                    GpiKpi oldGpiKpiOfConsequentCollectionNewConsequent = consequentCollectionNewConsequent.getGpiKpi();
                    consequentCollectionNewConsequent.setGpiKpi(gpiKpi);
                    consequentCollectionNewConsequent = em.merge(consequentCollectionNewConsequent);
                    if (oldGpiKpiOfConsequentCollectionNewConsequent != null && !oldGpiKpiOfConsequentCollectionNewConsequent.equals(gpiKpi)) {
                        oldGpiKpiOfConsequentCollectionNewConsequent.getConsequentCollection().remove(consequentCollectionNewConsequent);
                        oldGpiKpiOfConsequentCollectionNewConsequent = em.merge(oldGpiKpiOfConsequentCollectionNewConsequent);
                    }
                }
            }
            for (Thresholds thresholdsCollectionNewThresholds : thresholdsCollectionNew) {
                if (!thresholdsCollectionOld.contains(thresholdsCollectionNewThresholds)) {
                    GpiKpi oldGpiKpiOfThresholdsCollectionNewThresholds = thresholdsCollectionNewThresholds.getGpiKpi();
                    thresholdsCollectionNewThresholds.setGpiKpi(gpiKpi);
                    thresholdsCollectionNewThresholds = em.merge(thresholdsCollectionNewThresholds);
                    if (oldGpiKpiOfThresholdsCollectionNewThresholds != null && !oldGpiKpiOfThresholdsCollectionNewThresholds.equals(gpiKpi)) {
                        oldGpiKpiOfThresholdsCollectionNewThresholds.getThresholdsCollection().remove(thresholdsCollectionNewThresholds);
                        oldGpiKpiOfThresholdsCollectionNewThresholds = em.merge(oldGpiKpiOfThresholdsCollectionNewThresholds);
                    }
                }
            }
            for (GpiKpiInstance gpiKpiInstanceCollectionNewGpiKpiInstance : gpiKpiInstanceCollectionNew) {
                if (!gpiKpiInstanceCollectionOld.contains(gpiKpiInstanceCollectionNewGpiKpiInstance)) {
                    GpiKpi oldIdGPIKPIOfGpiKpiInstanceCollectionNewGpiKpiInstance = gpiKpiInstanceCollectionNewGpiKpiInstance.getIdGPIKPI();
                    gpiKpiInstanceCollectionNewGpiKpiInstance.setIdGPIKPI(gpiKpi);
                    gpiKpiInstanceCollectionNewGpiKpiInstance = em.merge(gpiKpiInstanceCollectionNewGpiKpiInstance);
                    if (oldIdGPIKPIOfGpiKpiInstanceCollectionNewGpiKpiInstance != null && !oldIdGPIKPIOfGpiKpiInstanceCollectionNewGpiKpiInstance.equals(gpiKpi)) {
                        oldIdGPIKPIOfGpiKpiInstanceCollectionNewGpiKpiInstance.getGpiKpiInstanceCollection().remove(gpiKpiInstanceCollectionNewGpiKpiInstance);
                        oldIdGPIKPIOfGpiKpiInstanceCollectionNewGpiKpiInstance = em.merge(oldIdGPIKPIOfGpiKpiInstanceCollectionNewGpiKpiInstance);
                    }
                }
            }
            for (GpiKpiInstance gpiKpiInstanceCollection1OldGpiKpiInstance : gpiKpiInstanceCollection1Old) {
                if (!gpiKpiInstanceCollection1New.contains(gpiKpiInstanceCollection1OldGpiKpiInstance)) {
                    gpiKpiInstanceCollection1OldGpiKpiInstance.setIdReferredGPIKPI(null);
                    gpiKpiInstanceCollection1OldGpiKpiInstance = em.merge(gpiKpiInstanceCollection1OldGpiKpiInstance);
                }
            }
            for (GpiKpiInstance gpiKpiInstanceCollection1NewGpiKpiInstance : gpiKpiInstanceCollection1New) {
                if (!gpiKpiInstanceCollection1Old.contains(gpiKpiInstanceCollection1NewGpiKpiInstance)) {
                    GpiKpi oldIdReferredGPIKPIOfGpiKpiInstanceCollection1NewGpiKpiInstance = gpiKpiInstanceCollection1NewGpiKpiInstance.getIdReferredGPIKPI();
                    gpiKpiInstanceCollection1NewGpiKpiInstance.setIdReferredGPIKPI(gpiKpi);
                    gpiKpiInstanceCollection1NewGpiKpiInstance = em.merge(gpiKpiInstanceCollection1NewGpiKpiInstance);
                    if (oldIdReferredGPIKPIOfGpiKpiInstanceCollection1NewGpiKpiInstance != null && !oldIdReferredGPIKPIOfGpiKpiInstanceCollection1NewGpiKpiInstance.equals(gpiKpi)) {
                        oldIdReferredGPIKPIOfGpiKpiInstanceCollection1NewGpiKpiInstance.getGpiKpiInstanceCollection1().remove(gpiKpiInstanceCollection1NewGpiKpiInstance);
                        oldIdReferredGPIKPIOfGpiKpiInstanceCollection1NewGpiKpiInstance = em.merge(oldIdReferredGPIKPIOfGpiKpiInstanceCollection1NewGpiKpiInstance);
                    }
                }
            }
            for (ExecutedRules executedRulesCollectionNewExecutedRules : executedRulesCollectionNew) {
                if (!executedRulesCollectionOld.contains(executedRulesCollectionNewExecutedRules)) {
                    GpiKpi oldIdGPIKPIViolatedOfExecutedRulesCollectionNewExecutedRules = executedRulesCollectionNewExecutedRules.getIdGPIKPIViolated();
                    executedRulesCollectionNewExecutedRules.setIdGPIKPIViolated(gpiKpi);
                    executedRulesCollectionNewExecutedRules = em.merge(executedRulesCollectionNewExecutedRules);
                    if (oldIdGPIKPIViolatedOfExecutedRulesCollectionNewExecutedRules != null && !oldIdGPIKPIViolatedOfExecutedRulesCollectionNewExecutedRules.equals(gpiKpi)) {
                        oldIdGPIKPIViolatedOfExecutedRulesCollectionNewExecutedRules.getExecutedRulesCollection().remove(executedRulesCollectionNewExecutedRules);
                        oldIdGPIKPIViolatedOfExecutedRulesCollectionNewExecutedRules = em.merge(oldIdGPIKPIViolatedOfExecutedRulesCollectionNewExecutedRules);
                    }
                }
            }
            for (Antecedent antecedentCollectionNewAntecedent : antecedentCollectionNew) {
                if (!antecedentCollectionOld.contains(antecedentCollectionNewAntecedent)) {
                    GpiKpi oldGpiKpiOfAntecedentCollectionNewAntecedent = antecedentCollectionNewAntecedent.getGpiKpi();
                    antecedentCollectionNewAntecedent.setGpiKpi(gpiKpi);
                    antecedentCollectionNewAntecedent = em.merge(antecedentCollectionNewAntecedent);
                    if (oldGpiKpiOfAntecedentCollectionNewAntecedent != null && !oldGpiKpiOfAntecedentCollectionNewAntecedent.equals(gpiKpi)) {
                        oldGpiKpiOfAntecedentCollectionNewAntecedent.getAntecedentCollection().remove(antecedentCollectionNewAntecedent);
                        oldGpiKpiOfAntecedentCollectionNewAntecedent = em.merge(oldGpiKpiOfAntecedentCollectionNewAntecedent);
                    }
                }
            }
            for (Correlation correlationCollectionNewCorrelation : correlationCollectionNew) {
                if (!correlationCollectionOld.contains(correlationCollectionNewCorrelation)) {
                    GpiKpi oldIdGPIKPI2OfCorrelationCollectionNewCorrelation = correlationCollectionNewCorrelation.getIdGPIKPI2();
                    correlationCollectionNewCorrelation.setIdGPIKPI2(gpiKpi);
                    correlationCollectionNewCorrelation = em.merge(correlationCollectionNewCorrelation);
                    if (oldIdGPIKPI2OfCorrelationCollectionNewCorrelation != null && !oldIdGPIKPI2OfCorrelationCollectionNewCorrelation.equals(gpiKpi)) {
                        oldIdGPIKPI2OfCorrelationCollectionNewCorrelation.getCorrelationCollection().remove(correlationCollectionNewCorrelation);
                        oldIdGPIKPI2OfCorrelationCollectionNewCorrelation = em.merge(oldIdGPIKPI2OfCorrelationCollectionNewCorrelation);
                    }
                }
            }
            for (Correlation correlationCollection1NewCorrelation : correlationCollection1New) {
                if (!correlationCollection1Old.contains(correlationCollection1NewCorrelation)) {
                    GpiKpi oldIdGPIKPI1OfCorrelationCollection1NewCorrelation = correlationCollection1NewCorrelation.getIdGPIKPI1();
                    correlationCollection1NewCorrelation.setIdGPIKPI1(gpiKpi);
                    correlationCollection1NewCorrelation = em.merge(correlationCollection1NewCorrelation);
                    if (oldIdGPIKPI1OfCorrelationCollection1NewCorrelation != null && !oldIdGPIKPI1OfCorrelationCollection1NewCorrelation.equals(gpiKpi)) {
                        oldIdGPIKPI1OfCorrelationCollection1NewCorrelation.getCorrelationCollection1().remove(correlationCollection1NewCorrelation);
                        oldIdGPIKPI1OfCorrelationCollection1NewCorrelation = em.merge(oldIdGPIKPI1OfCorrelationCollection1NewCorrelation);
                    }
                }
            }
            for (StoredValues storedValuesCollectionNewStoredValues : storedValuesCollectionNew) {
                if (!storedValuesCollectionOld.contains(storedValuesCollectionNewStoredValues)) {
                    GpiKpi oldIdGPIKPIOfStoredValuesCollectionNewStoredValues = storedValuesCollectionNewStoredValues.getIdGPIKPI();
                    storedValuesCollectionNewStoredValues.setIdGPIKPI(gpiKpi);
                    storedValuesCollectionNewStoredValues = em.merge(storedValuesCollectionNewStoredValues);
                    if (oldIdGPIKPIOfStoredValuesCollectionNewStoredValues != null && !oldIdGPIKPIOfStoredValuesCollectionNewStoredValues.equals(gpiKpi)) {
                        oldIdGPIKPIOfStoredValuesCollectionNewStoredValues.getStoredValuesCollection().remove(storedValuesCollectionNewStoredValues);
                        oldIdGPIKPIOfStoredValuesCollectionNewStoredValues = em.merge(oldIdGPIKPIOfStoredValuesCollectionNewStoredValues);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = gpiKpi.getId();
                if (findGpiKpi(id) == null) {
                    throw new NonexistentEntityException("The gpiKpi with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            GpiKpi gpiKpi;
            try {
                gpiKpi = em.getReference(GpiKpi.class, id);
                gpiKpi.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The gpiKpi with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Consequent> consequentCollectionOrphanCheck = gpiKpi.getConsequentCollection();
            for (Consequent consequentCollectionOrphanCheckConsequent : consequentCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This GpiKpi (" + gpiKpi + ") cannot be destroyed since the Consequent " + consequentCollectionOrphanCheckConsequent + " in its consequentCollection field has a non-nullable gpiKpi field.");
            }
            Collection<Thresholds> thresholdsCollectionOrphanCheck = gpiKpi.getThresholdsCollection();
            for (Thresholds thresholdsCollectionOrphanCheckThresholds : thresholdsCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This GpiKpi (" + gpiKpi + ") cannot be destroyed since the Thresholds " + thresholdsCollectionOrphanCheckThresholds + " in its thresholdsCollection field has a non-nullable gpiKpi field.");
            }
            Collection<GpiKpiInstance> gpiKpiInstanceCollectionOrphanCheck = gpiKpi.getGpiKpiInstanceCollection();
            for (GpiKpiInstance gpiKpiInstanceCollectionOrphanCheckGpiKpiInstance : gpiKpiInstanceCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This GpiKpi (" + gpiKpi + ") cannot be destroyed since the GpiKpiInstance " + gpiKpiInstanceCollectionOrphanCheckGpiKpiInstance + " in its gpiKpiInstanceCollection field has a non-nullable idGPIKPI field.");
            }
            Collection<ExecutedRules> executedRulesCollectionOrphanCheck = gpiKpi.getExecutedRulesCollection();
            for (ExecutedRules executedRulesCollectionOrphanCheckExecutedRules : executedRulesCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This GpiKpi (" + gpiKpi + ") cannot be destroyed since the ExecutedRules " + executedRulesCollectionOrphanCheckExecutedRules + " in its executedRulesCollection field has a non-nullable idGPIKPIViolated field.");
            }
            Collection<Antecedent> antecedentCollectionOrphanCheck = gpiKpi.getAntecedentCollection();
            for (Antecedent antecedentCollectionOrphanCheckAntecedent : antecedentCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This GpiKpi (" + gpiKpi + ") cannot be destroyed since the Antecedent " + antecedentCollectionOrphanCheckAntecedent + " in its antecedentCollection field has a non-nullable gpiKpi field.");
            }
            Collection<Correlation> correlationCollectionOrphanCheck = gpiKpi.getCorrelationCollection();
            for (Correlation correlationCollectionOrphanCheckCorrelation : correlationCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This GpiKpi (" + gpiKpi + ") cannot be destroyed since the Correlation " + correlationCollectionOrphanCheckCorrelation + " in its correlationCollection field has a non-nullable idGPIKPI2 field.");
            }
            Collection<Correlation> correlationCollection1OrphanCheck = gpiKpi.getCorrelationCollection1();
            for (Correlation correlationCollection1OrphanCheckCorrelation : correlationCollection1OrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This GpiKpi (" + gpiKpi + ") cannot be destroyed since the Correlation " + correlationCollection1OrphanCheckCorrelation + " in its correlationCollection1 field has a non-nullable idGPIKPI1 field.");
            }
            Collection<StoredValues> storedValuesCollectionOrphanCheck = gpiKpi.getStoredValuesCollection();
            for (StoredValues storedValuesCollectionOrphanCheckStoredValues : storedValuesCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This GpiKpi (" + gpiKpi + ") cannot be destroyed since the StoredValues " + storedValuesCollectionOrphanCheckStoredValues + " in its storedValuesCollection field has a non-nullable idGPIKPI field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Predicate> predicateCollection = gpiKpi.getPredicateCollection();
            for (Predicate predicateCollectionPredicate : predicateCollection) {
                predicateCollectionPredicate.getGpiKpiCollection().remove(gpiKpi);
                predicateCollectionPredicate = em.merge(predicateCollectionPredicate);
            }
            Collection<GpiKpiInstance> gpiKpiInstanceCollection1 = gpiKpi.getGpiKpiInstanceCollection1();
            for (GpiKpiInstance gpiKpiInstanceCollection1GpiKpiInstance : gpiKpiInstanceCollection1) {
                gpiKpiInstanceCollection1GpiKpiInstance.setIdReferredGPIKPI(null);
                gpiKpiInstanceCollection1GpiKpiInstance = em.merge(gpiKpiInstanceCollection1GpiKpiInstance);
            }
            em.remove(gpiKpi);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<GpiKpi> findGpiKpiEntities() {
        return findGpiKpiEntities(true, -1, -1);
    }

    public List<GpiKpi> findGpiKpiEntities(int maxResults, int firstResult) {
        return findGpiKpiEntities(false, maxResults, firstResult);
    }

    private List<GpiKpi> findGpiKpiEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(GpiKpi.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public GpiKpi findGpiKpi(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(GpiKpi.class, id);
        } finally {
            em.close();
        }
    }

    public int getGpiKpiCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<GpiKpi> rt = cq.from(GpiKpi.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
